package com.bootdo.appsystem.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.appsystem.domain.UseraccountDO;
import com.bootdo.appsystem.service.UseraccountService;
import com.bootdo.common.annotation.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.appsystem.domain.UserdetailsDO;
import com.bootdo.appsystem.service.UserdetailsService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author xiaoxiao
 * @email 806191255@qq.com
 * @date 2018-05-12 14:42:14
 */
 
@Controller
@RequestMapping("/appsystem/userdetails")
public class UserdetailsController {
	@Autowired
	private UserdetailsService userdetailsService;
	@Autowired
	private UseraccountService useraccountService;
	
	@GetMapping()
	@RequiresPermissions("appsystem:userdetails:userdetails")
	String Userdetails(){
	    return "appsystem/userdetails/userdetails";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("appsystem:userdetails:userdetails")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UserdetailsDO> userdetailsList = userdetailsService.list(query);
		int total = userdetailsService.count(query);
		PageUtils pageUtils = new PageUtils(userdetailsList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("appsystem:userdetails:add")
	String add(){
	    return "appsystem/userdetails/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("appsystem:userdetails:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		UserdetailsDO userdetails = userdetailsService.get(id);
		model.addAttribute("userdetails", userdetails);
	    return "appsystem/userdetails/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("appsystem:userdetails:add")
	public R save( UserdetailsDO userdetails){
		if(userdetailsService.save(userdetails)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
    @Log("审核身份")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("appsystem:userdetails:edit")
	public R update( UserdetailsDO userdetails){
		if(userdetails.getIdcardstatus() == 1){
            UseraccountDO useraccount = new UseraccountDO();
            useraccount.setUid(userdetails.getUid());
            useraccount.setSafeanswer(userdetails.getRealname());
            useraccountService.update(useraccount);
        }
		userdetailsService.update(userdetails);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("appsystem:userdetails:remove")
	public R remove( Integer id){
		if(userdetailsService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("appsystem:userdetails:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		userdetailsService.batchRemove(ids);
		return R.ok();
	}
	
}
